#include "stdafx.h"
#include "IntValidator.h"

IntValidator::IntValidator(int *value)
	: wxTextValidator(wxFILTER_NUMERIC), value(value)
{
}

IntValidator::IntValidator(const IntValidator &val)
	: wxTextValidator(val), value(val.value)
{
}

IntValidator::~IntValidator()
{
}

wxValidator *IntValidator::Clone() const
{
	return new IntValidator(*this);
}

bool IntValidator::TransferFromWindow()
{
	if(!value)
		return true;

	wxWindow *ctrl = GetWindow();
	ASSERT(ctrl);

	// Read the control value.
	wxString value;
	if(ctrl->IsKindOf(CLASSINFO(wxTextCtrl)))
	{
		wxTextCtrl *textCtrl = static_cast<wxTextCtrl*> (ctrl);
		value = textCtrl->GetValue();
	}
	else if(ctrl->IsKindOf(CLASSINFO(wxComboBox)))
	{
		wxComboBox *combo = static_cast<wxComboBox*> (ctrl);
		value = combo->GetValue();
	}
	else
	{
		ASSERT(NULL && "int validator only supports text and combo box.");
	}

	long temp;
	bool ret = value.ToLong(&temp);
	if(!ret)
		return false;

	*this->value = temp;
	return true;
}

bool IntValidator::TransferToWindow()
{
	if(!value)
		return false;

	// Create the text value.
	wxString textValue = wxString::Format(wxT("%d"), *value);

	// Set the value.
	wxWindow *ctrl = GetWindow();
	ASSERT(ctrl);
	if(ctrl->IsKindOf(CLASSINFO(wxTextCtrl)))
	{
		wxTextCtrl *textCtrl = static_cast<wxTextCtrl*> (ctrl);
		textCtrl->SetValue(textValue);
	}
	else if(ctrl->IsKindOf(CLASSINFO(wxComboBox)))
	{
		wxComboBox *combo = static_cast<wxComboBox*> (ctrl);
		combo->SetValue(textValue);
	}
	else
	{
		ASSERT(NULL && "int validator only supports text and combo box.");
	}

	return true;
}
